/*
 * DAdicionarEspecies.java
 *
 * Created on 12 de junio de 2006, 08:52 AM
 */

package ig.metrica.ui.modelo;

import ig.util.MyTableModel;
import java.util.Enumeration;
import java.util.Hashtable;

/**
 *
 * @author  jsanchez
 */
public class DAdicionarEspecies extends javax.swing.JDialog {
    
    /** Creates new form DAdicionarEspecies */
    String[] nameColumnas={"Especie","Factor Riesgo","Seleccionar"};
    private boolean[] editColumnas={false,false,true};
    private Object panel=null;
    private Hashtable httmp=null;
    public DAdicionarEspecies(java.awt.Frame parent,Hashtable httmp, Object panel,String quien) {
        super(parent, true);
        this.httmp=httmp;
        this.panel=panel;
        initComponents();
        nameColumnas[0]=quien;
        setDataTable(httmp);
        centrar();
        this.setVisible(true);
    }
    
     
    private void setData(){
        if(panel instanceof PantallaAsociacionFactorContrato )
            ((PantallaAsociacionFactorContrato)panel).setDataUpdate(((MyTableModel)this.Tabla.getModel()).getData());
        else if(panel instanceof PantallaAsociacionFactoresMatriz )
            ((PantallaAsociacionFactoresMatriz)panel).setDataUpdate(((MyTableModel)this.Tabla.getModel()).getData());
    }
    
    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jScrollPane2 = new javax.swing.JScrollPane();
        jTextPane1 = new javax.swing.JTextPane();
        jScrollPane1 = new javax.swing.JScrollPane();
        Tabla = new javax.swing.JTable();
        Cancelar = new javax.swing.JButton();
        Aceptar = new javax.swing.JButton();
        CheckAll = new javax.swing.JCheckBox();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        addWindowListener(new java.awt.event.WindowAdapter() {
            public void windowClosing(java.awt.event.WindowEvent evt) {
                formWindowClosing(evt);
            }
        });

        jScrollPane2.setBorder(null);

        jTextPane1.setBackground(javax.swing.UIManager.getDefaults().getColor("control"));
        jTextPane1.setBorder(null);
        jTextPane1.setEditable(false);
        jTextPane1.setText("El modelo seleccionado no tiene relacion con los siguientes Objetos, seleccione los Objetos que desea relacionar al  modelo");
        jScrollPane2.setViewportView(jTextPane1);

        Tabla.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseReleased(java.awt.event.MouseEvent evt) {
                TablaMouseReleased(evt);
            }
        });
        jScrollPane1.setViewportView(Tabla);

        Cancelar.setText("Cancelar");
        Cancelar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                CancelarActionPerformed(evt);
            }
        });

        Aceptar.setText("Aceptar");
        Aceptar.setEnabled(false);
        Aceptar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                AceptarActionPerformed(evt);
            }
        });

        CheckAll.setText("Activar/Desactivar -  Todos");
        CheckAll.setBorder(javax.swing.BorderFactory.createEmptyBorder(0, 0, 0, 0));
        CheckAll.setHorizontalAlignment(javax.swing.SwingConstants.RIGHT);
        CheckAll.setHorizontalTextPosition(javax.swing.SwingConstants.LEADING);
        CheckAll.setMargin(new java.awt.Insets(0, 0, 0, 0));
        CheckAll.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                CheckAllActionPerformed(evt);
            }
        });

        org.jdesktop.layout.GroupLayout layout = new org.jdesktop.layout.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(layout.createSequentialGroup()
                .addContainerGap()
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                    .add(layout.createSequentialGroup()
                        .add(jScrollPane2, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 410, Short.MAX_VALUE)
                        .addContainerGap())
                    .add(layout.createSequentialGroup()
                        .add(jScrollPane1, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 410, Short.MAX_VALUE)
                        .addContainerGap())
                    .add(org.jdesktop.layout.GroupLayout.TRAILING, layout.createSequentialGroup()
                        .add(Aceptar)
                        .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                        .add(Cancelar)
                        .add(107, 107, 107))
                    .add(org.jdesktop.layout.GroupLayout.TRAILING, layout.createSequentialGroup()
                        .add(CheckAll, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 410, Short.MAX_VALUE)
                        .addContainerGap())))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(layout.createSequentialGroup()
                .addContainerGap()
                .add(jScrollPane2, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 47, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(CheckAll)
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(jScrollPane1, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 156, Short.MAX_VALUE)
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                    .add(Cancelar)
                    .add(Aceptar))
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void CheckAllActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_CheckAllActionPerformed
     if(CheckAll.isSelected()){
         seleccion=true;
         this.Aceptar.setEnabled(seleccion);
     }else{
         seleccion=false;
         this.Aceptar.setEnabled(seleccion);
     }
     this.setDataTable(this.httmp);
     
    }//GEN-LAST:event_CheckAllActionPerformed

    private void TablaMouseReleased(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_TablaMouseReleased
        this.Aceptar.setEnabled(false);        
        for (int i = 0; i < ((MyTableModel)this.Tabla.getModel()).getData().length; i++) {
            if(((Boolean)((MyTableModel)this.Tabla.getModel()).getData()[i][2])){
                this.Aceptar.setEnabled(true);
                break;
            }
        }
    }//GEN-LAST:event_TablaMouseReleased

    private void formWindowClosing(java.awt.event.WindowEvent evt) {//GEN-FIRST:event_formWindowClosing
        this.cancelar();
    }//GEN-LAST:event_formWindowClosing

    private void CancelarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_CancelarActionPerformed
        cancelar();
    }//GEN-LAST:event_CancelarActionPerformed

    private void AceptarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_AceptarActionPerformed
        aceptar();
    }//GEN-LAST:event_AceptarActionPerformed

    private void aceptar() {
        this.setData();
        this.dispose();
    }
    private void cancelar() {
        Object[][] data= {};
        if(panel instanceof PantallaAsociacionFactorContrato )
            ((PantallaAsociacionFactorContrato)panel).setDataUpdate(data);
        else if(panel instanceof PantallaAsociacionFactoresMatriz )
            ((PantallaAsociacionFactoresMatriz)panel).setDataUpdate(data);
        this.dispose();
    }
    public void centrar() {
        java.awt.Dimension dimensionScreen = java.awt.Toolkit.getDefaultToolkit().getScreenSize();
        java.awt.Rectangle dimensionPantalla = getBounds();
        setLocation((dimensionScreen.width - dimensionPantalla.width) / 2, (dimensionScreen.height - dimensionPantalla.height) / 2);
    }        
    boolean seleccion=false;
    private void setDataTable(Hashtable httmp) {
        Object[][] Data= new Object[httmp.size()][3];
        Enumeration enumeration= httmp.keys();
        int i=0;
        while(enumeration.hasMoreElements()){
            Object key=enumeration.nextElement();            
            Data[i][0]=key;
            Data[i][1]=((ig.metrica.riesgo.FactorRiesgoPersonalMetrica)httmp.get(key)).getFactorRiesgoMatrizCorrelacion().getNombre();
            Data[i][2]=seleccion;
            i++;
        }
        this.Tabla.setModel(new MyTableModel(Data, this.nameColumnas,this.editColumnas));
        Tabla.getTableHeader().setReorderingAllowed(false);
    }
    
//    /**
//     * @param args the command line arguments
//     */
//    public static void main(String args[]) {
//        java.awt.EventQueue.invokeLater(new Runnable() {
//            public void run() {
//                new DAdicionarEspecies(new javax.swing.JFrame()).setVisible(true);
//            }
//        });
//    }
    
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton Aceptar;
    private javax.swing.JButton Cancelar;
    private javax.swing.JCheckBox CheckAll;
    private javax.swing.JTable Tabla;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JTextPane jTextPane1;
    // End of variables declaration//GEN-END:variables
    
}
